package com.deram.ctin.serv.search.solr.impl;

import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.serv.business.api.BusinessInfoApi;
import com.deram.ctin.serv.business.entity.BusinessInfo;
import com.deram.ctin.serv.enter.api.EnterInfoApi;
import com.deram.ctin.serv.enter.entity.EnterInfo;
import com.deram.ctin.serv.search.entity.solr.api.SolrEnterInfoApi;
import com.deram.ctin.serv.search.entity.solr.bean.SolrCustomsInfo;
import com.deram.ctin.serv.search.entity.solr.bean.SolrEnterInfo;
import com.deram.ctin.serv.search.entity.solr.search.WebEnterSearchParam;
import com.deram.ctin.serv.search.solr.service.SolrCustomsInfoService;
import com.deram.ctin.serv.search.solr.service.SolrEnterInfoService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.query.result.SolrResultPage;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class SolrEnterInfoApiImpl implements SolrEnterInfoApi {

    @Autowired
    private SolrEnterInfoService solrEnterInfoService;

    @Autowired
    private BusinessInfoApi businessInfoApi;

    @Autowired
    private SolrCustomsInfoService solrCustomsInfoService;

    @Autowired
    private EnterInfoApi enterInfoApi;
    @Override
    public SolrEnterInfo find(@RequestParam("id") Long id) {
        return solrEnterInfoService.findById(id);
    }

    @Override
    public  List<SolrEnterInfo> findAll() {
        return solrEnterInfoService.findAll();
    }

    @Override
    public PageResult<SolrEnterInfo> searchByKeywords(@RequestBody WebEnterSearchParam keywords, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize)  {
        return solrEnterInfoService.searchByKeywords(keywords,pageNo,pageSize);
    }


    @Override
    public SolrEnterInfo save() {


        SolrEnterInfo solrEnterInfo = new SolrEnterInfo();
//        EnterInfo param = (EnterInfo) map.get("enter");
//        BusinessInfo businessInfo = (EnterInfo) map.get("enter");
//        solrEnterInfo.setAddress(param.getAddress());
//        solrEnterInfo.setEmail(param.getEmail());
//        solrEnterInfo.setEnterCode(param.getEnterCode());
//        solrEnterInfo.setFatherId(param.getFatherId());
//        solrEnterInfo.setId(param.getId());
//        solrEnterInfo.setKeywords(param.getKeywords());
//        solrEnterInfo.setLegalCode(param.getLegalCode());
//        solrEnterInfo.setLegalName(param.getLegalName());
//        solrEnterInfo.setListed(param.getListed());
//        solrEnterInfo.setLogoPath(param.getLogoPath());
//        solrEnterInfo.setName(param.getName());
//        solrEnterInfo.setTelephone(param.getTelephone());
//        solrEnterInfo.setUpdateDate(param.getUpdateDate());
//        solrEnterInfo.setWebsite(param.getWebsite());
//        solrEnterInfo.setOpeningTime(openingTime);
//        solrEnterInfo.setClosingTime(closingTime);
//        solrEnterInfo.setRegisteredCapital(registeredCapital.doubleValue());
//        solrEnterInfo.setCreationDate();
//        solrEnterInfo.setIndustryInvolved();
//        solrEnterInfo.setAreaName();
//        solrEnterInfo.setManagementStatus();
//        solrEnterInfoService.save(solrEnterInfo);
        return solrEnterInfo;
    }

    @Override
    public void saveList(@RequestBody List<EnterInfo> infoList) {
        for (EnterInfo param:infoList) {
                SolrEnterInfo solrEnterInfo = new SolrEnterInfo();
                BusinessInfo businessInfo = businessInfoApi.find(param.getId());
                solrEnterInfo.setAddress(param.getAddress());
                solrEnterInfo.setEmail(param.getEmail());
                solrEnterInfo.setEnterCode(param.getEnterCode());
                solrEnterInfo.setFatherId(param.getFatherId());
                solrEnterInfo.setId(param.getId());
                solrEnterInfo.setEnterKeywords(param.getKeywords());
                solrEnterInfo.setLegalReps(param.getLegalRepsCode());
                solrEnterInfo.setLegalRepsCode(param.getLegalReps());
                solrEnterInfo.setListed(param.getListed());
                solrEnterInfo.setLogoPath(param.getLogoPath());
                solrEnterInfo.setName(param.getName());
                solrEnterInfo.setTelephone(param.getTelephone());
                solrEnterInfo.setUpdateDate(param.getUpdateDate());
                solrEnterInfo.setWebsite(param.getWebsite());
                solrEnterInfo.setOpeningTime(businessInfo.getOpeningTime());
                solrEnterInfo.setClosingTime(businessInfo.getClosingTime());
                solrEnterInfo.setRegisteredCapital(businessInfo.getRegisteredCapital().doubleValue());
                solrEnterInfo.setCreationDate(businessInfo.getCreationDate());
                solrEnterInfo.setIndustryInvolved(businessInfo.getIndustryInvolved());
                solrEnterInfo.setAreaName(businessInfo.getAreaName());
                solrEnterInfo.setManagementStatus(businessInfo.getManagementStatus());
                List<SolrCustomsInfo> solrCustomsInfoList = solrCustomsInfoService.findByEnter(param.getId());
                if (solrCustomsInfoList.size() > 0) {
                    solrEnterInfo.setHasCustoms(true);
                } else {
                    solrEnterInfo.setHasCustoms(false);
                }
                if (businessInfo != null) {
                    solrEnterInfo.setHasBusiness(true);
                } else {
                    solrEnterInfo.setHasBusiness(false);
                }
//                for (int i = 0; i < solrCustomsInfoList.size(); i++) {
//                    List<Double> dollarPriceList = Lists.newArrayList();
//                    dollarPriceList.add(solrCustomsInfoList.get(i).getDollarPrice().doubleValue());
//                    solrEnterInfo.setDollarPriceList(dollarPriceList);
//                }
                solrEnterInfoService.save(solrEnterInfo);
            }

    }

    @Override
    public void delete(@RequestParam("id") Long id) {
        solrEnterInfoService.delete(id);
    }
}